//
//  DYZhusuDetailViewController.swift
//  SheYangBigData
//
//  Created by 宋海胜 on 2021/1/11.
//  Copyright © 2021 叁拾叁. All rights reserved.
//




import UIKit
import GDPerformanceView_Swift

class DYZhusuDetailViewController: SYBaseViewController, UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate {

    let first = 301.5.autoWidth() + STATUSBAR_HEIGHT
    let sec = 597.5.autoWidth() + STATUSBAR_HEIGHT
    let thir = 945.5.autoWidth() + STATUSBAR_HEIGHT
    let four = 1100.5.autoWidth() + STATUSBAR_HEIGHT
    let five = 1296.5.autoWidth() + STATUSBAR_HEIGHT
    let six = 1446.5.autoWidth() + STATUSBAR_HEIGHT
    
    var firName: String = "酒店预订"
    var secName: String = "酒店详情"
    var thirName: String = "游客咨询"
    var fourName: String = "更多推荐"
    
    var firHeight: CGFloat = 0.0
    var secHeight: CGFloat = 0.0
    var thirHeight: CGFloat = 0.0
    var fourHeight: CGFloat = 0.0
    
    var firRect: CGRect = CGRect.zero
    var secRect: CGRect = CGRect.zero
    var thirRect: CGRect = CGRect.zero
    var fourRect: CGRect = CGRect.zero
    
    var labels: [String] = []
    
    var tableView: UITableView!
    let navView: SYBaseView = SYBaseView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: NAV_HEIGHT))
    let biaoqian: DYBiaoqianChoiceView = DYBiaoqianChoiceView(frame: CGRect(x: 0, y: NAV_HEIGHT, width: SCREEN_WIDTH, height: 40))
    let topView: DYJiudianHeadView = DYJiudianHeadView()
    var id: String!
    
    var model: DYZSDetailModel!
    
    init(_ id: String) {
        super.init(nibName: nil, bundle: nil)
        self.id = id
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.isHidden = true
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setTableView()
        topMangain()
        creatBiaoqian()
        getData()
    }
    
    public func topMangain() {
        // 解决Webview未覆盖顶部状态栏
        if #available(iOS 11.0, *) {
            tableView.contentInsetAdjustmentBehavior = UIScrollView.ContentInsetAdjustmentBehavior.never
        } else {
            self.edgesForExtendedLayout = UIRectEdge.bottom
        }
    }
    
    func getData() {
        requestVM.baseRequest(disposeBag: dispose, type: .dy_zhusuDetail(id: self.id), modelClass: SGBaseModel<DYZSDetailModel>.self) {[weak self] (res) in
            if let `self` = self {
                self.model = res.data

                if let m = self.model, m.roomList?.count != 0 {
                    self.labels.append(self.firName)
                }

                if let m = self.model, m.detail != nil {
                    self.labels.append(self.secName)
                }

                if let m = self.model, m.advisoryList  != nil {
                    self.labels.append(self.thirName)
                }
                
                if let m = self.model, m.tourHotelList  != nil {
                    self.labels.append(self.fourName)
                }

                self.tableView.reloadData()
                self.biaoqian.configureView(titles: self.labels)
                self.topView.configure(self.model)
            }
            
        } Error: {
            
        }
    }
    
    
    func creatBiaoqian() {
        
        navView.isHidden = true
        navView.lineView.isHidden = true
        navView.backgroundColor = .white
        view.addSubview(navView.lineView)
        navView.lineView.frame = CGRect(x: 0, y: NAV_HEIGHT - 0.5, width: SCREEN_WIDTH, height: 0.5)
        let back: UIButton = UIButton()
        back.addTarget(self, action: #selector(backAction), for: .touchUpInside)
        back.setImage(UIImage(named: "back"), for: .normal)
        view.addSubview(navView)
        navView.addSubview(back)
        back.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.top.equalToSuperview().offset(STATUSBAR_HEIGHT)
            make.height.equalTo(48.autoWidth())
            make.width.equalTo(30.autoWidth())
        }
        biaoqian.isHidden = true
        biaoqian.backgroundColor = .white
        biaoqian.clickedClosure = { index in
            
            var rect: CGRect = CGRect.zero
            
            switch self.labels[index - 100] {
            case self.firName:
                rect = self.firRect
            case self.secName:
                rect = self.secRect
            case self.thirName:
                rect = self.thirRect
            case self.fourName:
                rect = self.fourRect
            
            default:
                break
            }
            self.tableView.setContentOffset(CGPoint(x: 0, y: rect.minY - NAV_HEIGHT - 40), animated: true)
        }
        view.addSubview(biaoqian)
    }

    func setTableView() {
        
        //创建表格视图
        self.tableView = UITableView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT-BOTTOM_SAFE_HEIGHT), style:.grouped)
        self.tableView.separatorStyle = .none
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView!.register(DYJiudianYuyueCell.self, forCellReuseIdentifier: "DYJiudianYuyueCell")
        self.tableView!.register(DYJiudianDetailCell.self, forCellReuseIdentifier: "DYJiudianDetailCell")
        self.tableView!.register(DYYoukeZixunCell.self, forCellReuseIdentifier: "DYYoukeZixunCell")
        self.tableView!.register(DYZhusuTuijianCell.self, forCellReuseIdentifier: "DYZhusuTuijianCell")
        
        
        self.view.addSubview(self.tableView!)
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 4
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.row == 0 {
            let cell: DYJiudianYuyueCell? = tableView.dequeueReusableCell(withIdentifier: "DYJiudianYuyueCell") as? DYJiudianYuyueCell
            cell?.selectionStyle = .none
            cell!.currentVC = self
            if let m = self.model, let s = m.roomList, s.count != 0 {
                cell?.configureView(m)
            } else {
                cell?.isHidden = true
            }
            return cell!
        } else if indexPath.row == 1 {
            let cell: DYJiudianDetailCell? = tableView.dequeueReusableCell(withIdentifier: "DYJiudianDetailCell") as? DYJiudianDetailCell
            cell?.selectionStyle = .none
            cell!.currentVC = self
            if let m = self.model, m.detail != nil {
//                cell?.configureView(s)
            } else {
                cell?.isHidden = true
            }
            return cell!
        } else if indexPath.row == 2 {
            let cell: DYYoukeZixunCell? = tableView.dequeueReusableCell(withIdentifier: "DYYoukeZixunCell") as? DYYoukeZixunCell
            cell?.selectionStyle = .none
            cell!.currentVC = self
            if let m = self.model, let s = m.advisoryList, s.count != 0 {
                cell?.configureView(s)
            } else {
                cell?.isHidden = true
            }
            cell?.clickedClosure = {[weak self] _ in
                if let `self` = self {
                    let zixunModel = DYZixunModel()
                    zixunModel.id = self.model.id
                    zixunModel.name = self.model.name
                    zixunModel.imgUrl = self.model.imgUrl?.components(separatedBy: ",").first
                    let vc = DYZixunViewController(model: zixunModel, listType: .zhusu)
                    self.navigationController?.pushViewController(vc, animated: true)
                }
            }
            return cell!
        }  else {
               let cell: DYZhusuTuijianCell? = tableView.dequeueReusableCell(withIdentifier: "DYZhusuTuijianCell") as? DYZhusuTuijianCell
            cell?.selectionStyle = .none
            cell!.currentVC = self
            if let m = self.model, let s = m.tourHotelList, s.count != 0 {
                cell?.configureView(m)
            } else {
                cell?.isHidden = true
            }
               return cell!
        }
        
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        topView.currentVC = self
        return topView
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 301.5.autoWidth() + STATUSBAR_HEIGHT
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return UIView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 0.01))
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.01
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        switch indexPath.row {
        case 0:
            if let m = self.model, m.roomList?.count == 0 {
                return 0.0
            }
        case 1:
            if let m = self.model, m.detail == nil {
                return 0.0
            }
        case 2:
            if let m = self.model, m.advisoryList?.count == 0 {
                return 0.0
            }
        case 3:
            if let m = self.model, m.tourHotelList?.count == 0 {
                return 0.0
            }
        default:
            break
        }
        
        return UITableView.automaticDimension
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        let y = scrollView.contentOffset.y + 40 + NAV_HEIGHT
        
        firHeight = self.tableView.rectForHeader(inSection: 0).maxY
        
        secRect = self.tableView.rectForRow(at: IndexPath(row: 0, section: 0))
        thirRect = self.tableView.rectForRow(at: IndexPath(row: 1, section: 0))
        fourRect = self.tableView.rectForRow(at: IndexPath(row: 2, section: 0))
        
        secHeight = self.secRect.maxY
        thirHeight = self.thirRect.maxY
        fourHeight = self.fourRect.maxY
//        print("y: \(y)  headHeight:\(headHeight)  menpiaoHeight:\(menpiaoHeight)  chizhuHeight:\(chizhuHeight)  jiangjieHeight:\(jiangjieHeight)  youlanHeight:\(youlanHeight)  zixunHeight:\(zixunHeight)")
        
        if y > firHeight {
            biaoqian.isHidden = false
            navView.isHidden = false
            navView.lineView.isHidden = false
        } else {
            biaoqian.isHidden = true
            navView.isHidden = true
            navView.lineView.isHidden = true
        }
        
        if y >= firHeight && y < secHeight {
            biaoqian.refreshItems(self.firName)
        } else if y >= secHeight && y < thirHeight {
            biaoqian.refreshItems(self.secName)
        } else if y >= thirHeight && y < fourHeight {
            biaoqian.refreshItems(self.thirName)
        } else if y >= fourHeight {
            biaoqian.refreshItems(self.fourName)
            
        }
        
    }
    
    @objc func backAction() {
        self.navigationController?.popViewController(animated: true)
    }
}
